import { getFileJson, getFileZip } from '../../../services/report'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    reportInfo:{},
    batteryScoreList:[],
    batteryInfo:{},
    url:'https://xly.zr-power.cn/vehicle/#/battery-report?token=eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjRkN2M5MjAxLTE0YmItNDhjMi1iZGJiLWIwNjE1N2RmYmU3NSIsInVzZXJuYW1lIjoiYWRtaW4ifQ.9iA714P_PHafXd6iZOFibcCLFwiH208xZubUkIZZZs80Tea9rSnRNZIPXkWTRtXzST1CoXef4buWiySQNb-rsg'
  },

  onLoad(options) {
    if(!options){
      return
    }
    let jsonUrl = options.jsonUrl
    console.log("jsonUrl>>>", jsonUrl)
    // VehicleDetect/07a7ae3d-c2dcdc65/evalResult/20230815/07a7ae3d-c2dcdc65_20230815124405.zip
    // VehicleDetect/d52bff3-de51c5d6/evalResult/20230728/20230728114454.json
    // 'VehicleDetect/d52bff3-de51c5d6/evalResult/20230720/20230720130743.json'
    if(jsonUrl.indexOf('.json') > -1){
      this.getFileData(jsonUrl)
    } else {
      this.handleGetFileZip(jsonUrl)
    }
  },

  async getFileData(jsonUrl){
    let res = await getFileJson(jsonUrl)
    // 处理电池分项得分
    this.setBatteryInfo(res)

    this.setData({
      reportInfo: res
    })
  },

  async handleGetFileZip(url){
    let res = await getFileZip({ url })
    if(res.code == 200){
      let data = JSON.parse(res.msg)
      this.setBatteryInfo(data)
      this.setData({
        reportInfo: data
      })
    }
    // const zipData = await JSZip.loadAsync(res);
    // console.log("zipData>>", zipData)
    // zipData.forEach(async (relativePath, file) => {
    //   if (!file.dir) {
    //     const content = await file.async('string'); // 解压文件内容
    //     console.log("content>>>", content)
    //     let reportDetailInfo = JSON.parse(content);
    //     console.log("reportDetailInfo>>", reportDetailInfo)
    //     // 处理电池分项得分
    //     this.setBatteryInfo(reportDetailInfo)
    //     this.setData({
    //       reportInfo: reportDetailInfo
    //     })
    //   }
    // });
  },

  setBatteryInfo(res){
    let batteryScoreList = [
      res.sohValue || 0,
      res.bmsAccuracy || 0,
      res.alarmScore || 0,
      res.uniformityTmp || 0,
      res.uniformityV || 0,
      res.socAccuracy || 0,
    ];
    let batteryInfo = {
      sohValue: res.sohValue || 0, 
      bmsAccuracy: res.bmsAccuracy || 0,
      alarmScore: res.alarmScore || 0,
      uniformityTmp: res.uniformityTmp || 0,
      uniformityV: res.uniformityV || 0,
      socAccuracy: res.socAccuracy || 0,
    }
    this.setData({
      batteryScoreList,
      batteryInfo
    })
  }
})